"""
@File    :   validate.py
@Time    :   2024/04/24 23:14:22
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   OAuth2授权码模式相关的pydantic验证模型
"""

from pydantic import BaseModel, Field, HttpUrl, constr, field_serializer

from src.business.admin.system.oauth2.auth.enums import (
    UserLoginPasswordEncryptTypeEnum,
)
from src.schemas.serializer_func import http_url_serializer
from src.schemas.validate import NoUseEnumValuesBaseValidateSchemaConfigMixin


class AuthorizationCodeCreateValidateSchema(
    BaseModel, NoUseEnumValuesBaseValidateSchemaConfigMixin
):
    """用于创建OAuth2授权码模式的授权码的验证模型"""

    tenant_id: int = Field(default=..., description="租户ID")
    oauth2_client_client_id: str = Field(
        default=..., min_length=1, max_length=255, description="OAuth2客户端编号"
    )
    required_scopes: list[constr(min_length=1, max_length=100)] = Field(
        default_factory=list, description="用户选择的授权范围"
    )
    redirect_uri: HttpUrl = Field(default=..., description="重定向url")
    state: str = Field(
        default=..., min_length=1, max_length=200, description="客户端状态"
    )
    username: str = Field(
        default=..., min_length=1, max_length=20, description="登录用户名"
    )
    password: str = Field(
        default=..., min_length=1, max_length=500, description="登录密码"
    )
    encrypt_type: UserLoginPasswordEncryptTypeEnum = Field(
        default=UserLoginPasswordEncryptTypeEnum.rsa.value, description="密码加密方式"
    )
    # 字段序列化
    serializer_redirect_uri = field_serializer("redirect_uri", when_used="always")(
        http_url_serializer
    )


class AuthorizationCodeCreateDoubleTokenValidateSchema(
    BaseModel, NoUseEnumValuesBaseValidateSchemaConfigMixin
):
    """用于创建Oauth2授权码模式的双Token的验证模型"""

    code: str = Field(default=..., min_length=1, max_length=255, description="授权码")
    oauth2_client_client_id: str = Field(
        default=..., min_length=1, max_length=255, description="OAuth2客户端编号"
    )
    oauth2_client_client_secret: str = Field(
        default=..., min_length=1, max_length=255, description="OAuth2客户端密钥"
    )
